<template>
	<view class="P_2">
		<view class="bg_fff B_radius_5 P_2">
			<view class="fontS_12 PT_2">
				原密码
			</view>
			<view class="PTB_2 line_bom">
				<input v-model="pass" class="fontS_14" type="password" placeholder="请输入新密码" />
			</view>
			<view class="fontS_12 PT_2">
				新密码
			</view>
			<view class="PTB_2 line_bom">
				<input v-model="repass" class="fontS_14 W_30vw" type="password" placeholder="请输入新密码" />
			</view>
			<view class="fontS_12 PT_2">
				验证码
			</view>
			<view class="PTB_2 line_bom flex_row_between">
				<input v-model="code" class="fontS_14" type="number" placeholder="请输入验证码" />
				<view v-if="hasCode" class="fontS_12 B_radius_5" style="color: #ff0030">
				  {{ num + "秒后再试" }}
				</view>
				<view v-else @click="getVerify" class="fontS_12" style="color: #ff0030">验证码</view>
			</view>
			
		</view>
		
		<view @click="submit" class="pub_bg B_radius_5 color_fff flex_rowC PTB_2 MT_4">
			保存
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hasCode: false,
				num: 59,
				user:{},
				code:'',
				pass:'',
				repass:'',
			}
		},
		onLoad(){
			this.getUser()
		},
		methods: {
			submit(){
				let _this = this
				if(!this.pass){
					this.toast('请输入原密码')
				}else if(!this.repass){
					this.toast('请输入新密码')
				}else if(!this.code){
					this.toast('请输入验证码')
				}else{
					let params = {
						oldpwd:this.pass,
						password:this.repass,
						code:this.code,
					};
					this.$req('auth/changpwd',params,function(res){
						if(res.code==1){
							_this.toast('修改成功');
							setTimeout(function(){
								uni.navigateBack({
									delta:1
								})
							},500)
						}else{
							_this.toast(res.msg)
						}
					})
				}
			},
			getUser(){
				let _this = this;
				this.$req('auth/me',{},function(res){
					if(res.code==1){
						_this.user = res.data
					}else{
						_this.toast(res.msg)
					}
				})
			},
			getVerify() {
			  //获取验证码
			  let _this = this;
			 //  if (!this.mobile) {
			 //    uni.showToast({
				// 	title:'请先输入手机号'
				// });
			 //  } else {
			    this.$req('sms', { mobile: _this.user.mobile },(res) => {
			      if (res.code == 200) {
			        _this.hasCode = true;
			        //apiToast("验证码发送成功");
			        setInterval(() => {
			          if (_this.num > 0) {
			            _this.num--;
			          } else {
			            _this.hasCode = false;
			            _this.num = 59;
			          }
			        }, 1000);
			      } else {
			        // apiToast(res.msg);
			      }
			    });
			  // }
			}
		}
	}
</script>

<style>

</style>
